//
//  SAWAlertView.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/26.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "SAWAlertView.h"
@interface SAWAlertView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray *sourceArr;
@property(nonatomic,strong)NSMutableArray *typeArr;
@property(nonatomic,assign)NSInteger type;
@end
@implementation SAWAlertView
-(instancetype)initWithdataArr:(NSArray *)dataArr withType:(NSInteger)type
{
    self = [super init];
    if (self) {
        _type = type;
        [self setUI];
        if (type==100) {
           self.sourceArr = dataArr;
        }
        if (_type==1) {
            NSMutableArray *arr=@[].mutableCopy;
            NSMutableArray *arr1=@[].mutableCopy;
            for (NSDictionary *dic in dataArr) {
                [arr addObject:dic[@"subjectname"]];
                [arr1 addObject:dic[@"subjectid"]];
            }
            self.typeArr = arr1;
            self.sourceArr = arr;
        }
        if (_type==2) {
            NSMutableArray *arr=@[].mutableCopy;
            NSMutableArray *arr1=@[].mutableCopy;
            for (NSDictionary *dic in dataArr) {
                [arr addObject:dic[@"textbookname"]];
                [arr1 addObject:[NSString stringWithFormat:@"%@",dic[@"tid"]]];
            }
            self.typeArr = arr1;
            self.sourceArr = arr;
        }
        if (_type==3) {
            NSMutableArray *arr=@[].mutableCopy;
            NSMutableArray *arr1=@[].mutableCopy;
            for (NSDictionary *dic in dataArr) {
                [arr addObject:dic[@"gradename"]];
                [arr1 addObject:dic[@"gradeid"]];
            }
            self.typeArr = arr1;
            self.sourceArr = arr;
        }
        if (_type==6) {
            NSMutableArray *arr=@[].mutableCopy;
            NSMutableArray *arr1=@[].mutableCopy;
            for (NSDictionary *dic in dataArr) {
                [arr addObject:dic[@"unit_name"]];
                [arr1 addObject:dic[@"id"]];
            }
            self.typeArr = arr1;
            self.sourceArr = arr;
        }
        if (_type==8) {
            NSMutableArray *arr=@[].mutableCopy;
            NSMutableArray *arr1=@[].mutableCopy;
            for (NSDictionary *dic in dataArr) {
                [arr addObject:dic[@"knowledgeName"]];
                [arr1 addObject:dic[@"id"]];
            }
            self.typeArr = arr1;
            self.sourceArr = arr;
        }
    }
    return self;
}
-(void)setUI
{
   
    //[@"subjectname"]
    //[@"gradename"]
//[@"textbookname"]
    UILabel *titLab =[[UILabel alloc]init];
    [self addSubview:titLab];
    titLab.text=@"请选择";
    titLab.backgroundColor=[UIColor whiteColor];
    titLab.textAlignment=1;
    titLab.textColor=[UIColor greenColor];
    [titLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(25);
        make.right.offset(-25);
        make.top.offset(86);
        make.height.offset(45);
        
    }];
    
    UITableView *tableView = [[UITableView alloc]init];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:tableView];
    tableView.delegate=self;
    tableView.dataSource=self;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(25);
        make.right.offset(-25);
        make.top.equalTo(titLab.mas_bottom).offset(0);
        make.height.offset(kScreenHeight-66-136);
    }];
    
//    UIButton *comitBtn = [[UIButton alloc]init];
//    [self addSubview:comitBtn];
//    comitBtn.backgroundColor=[UIColor whiteColor];
//    [comitBtn setTitle:@"确定" forState:0];
//    [comitBtn addTarget:self action:@selector(comitData) forControlEvents:UIControlEventTouchUpInside];
//    [comitBtn setTitleColor:[UIColor greenColor] forState:0];
//    [comitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(25);
//        make.right.offset(-25);
//        make.top.equalTo(tableView.mas_bottom).offset(0);
//        make.height.offset(33);
//    }];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selecItem) {
        if (_type==100) {
          self.selecItem(self.sourceArr[indexPath.row],0);
        }
        else{
          self.selecItem(self.sourceArr[indexPath.row],self.typeArr[indexPath.row]);
        }
    }
    
    [self dismiss];
}
//-(void)comitData
//{
//    [self dismiss];
//}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text=self.sourceArr[indexPath.row];
    cell.textLabel.textAlignment=1;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
